# Note: To execute this code outside the 02_Replicate.R, load packages by
# uncommenting #library() commands, set working directory to replication folder 
# using setwd(). Then load the datasets by uncommenting the commands loading the 
# data.


#library("dplyr")
#library("ggplot2")
#library("lfe")
#library("estimatr")
#library("stargazer")
#library("mvtnorm")
#library("starbility")
#library("gridExtra")
#library("xtable")
#library("magrittr")

#setwd() # set working directory to replication folder
#load("Data/rtl_data.Rdata")
#lh <- read.csv("Data/laver_hunt_party_org.csv")


# Appendix Tables -----------------------------------------------------

# Table A3:

describe <- function(var){
  nobs <- sum(!is.na(var))
  s <- summary(var)
  row <- c(nobs, s[c(4,1:3,5:6)])
  names(row)[1] <- "Observations"
  return(row)
}

# This is the subset of observations without missingness in the outcome or covariates
sset <- filter(dat, !is.na(delta_voteshare) & !is.na(lead_center))

tabA3 <- do.call("rbind",
  list(with(sset, describe(losepower)),
      with(sset, describe(to_extreme)),
      with(sset, describe(absolute_shift)),
      with(sset, describe(ls)),
      with(sset, describe(voteshare)),
      with(sset, describe(delta_voteshare)),
      with(sset, describe(outofcoalition)))) %>%
  as.data.frame() %>%
  mutate(Variable = c("Loss of Power", "To Extreme (t-1)", "Absolute shift (t-1)",
                       "Large Selectorate", "Voteshare (t)", "Change in voteshare (t)",
                       "Out of coalition (t)")) %>%
  select(Variable, Observations:`Max.`)

write(print(xtable(tabA3), include.rownames=FALSE), file = "Outputs/Table_A3.tex")


# Table A4 -- main platform coding (Equation 3)
platforms <- c("Left", "Center", "Right")
tabA4a <- prop.table(with(filter(dat, main_sample == 1 & losepower == 0), table(ternary_class, lead_ternary_class)), 1)
tabA4b <- prop.table(with(filter(dat, main_sample == 1 & losepower == 1), table(ternary_class, lead_ternary_class)), 1)
rownames(tabA4a) <- rownames(tabA4b) <- colnames(tabA4a) <- colnames(tabA4b) <- platforms
write(print(xtable(tabA4a), include.rownames=FALSE), file = "Outputs/Table_A4a.tex")
write(print(xtable(tabA4b), include.rownames=FALSE), file = "Outputs/Table_A4b.tex")



## Table A5 --
tabA5a <- prop.table(with(filter(dat, main_sample == 1 & losepower == 0), table(ternary_class_factorb, lead_ternary_classb)), 1)
tabA5b <- prop.table(with(filter(dat, main_sample == 1 & losepower == 1), table(ternary_class_factorb, lead_ternary_classb)), 1)
rownames(tabA5a) <- rownames(tabA5b) <- colnames(tabA5a) <- colnames(tabA5b) <- platforms
write(print(xtable(tabA5a), include.rownames=FALSE), file = "Outputs/Table_A5a.tex")
write(print(xtable(tabA5b), include.rownames=FALSE), file = "Outputs/Table_A5b.tex")


# Table A6:
tabA6a <- prop.table(with(filter(dat, main_sample == 1 & losepower == 0), table(alt_ternary_class_factor, lead_alt_ternaryclass)), 1)
tabA6b <- prop.table(with(filter(dat, main_sample == 1 & losepower == 1), table(alt_ternary_class_factor, lead_alt_ternaryclass)), 1)
rownames(tabA6a) <- rownames(tabA6b) <- colnames(tabA6a) <- colnames(tabA6b) <- platforms
write(print(xtable(tabA6a), include.rownames=FALSE), file = "Outputs/Table_A6a.tex")
write(print(xtable(tabA6b), include.rownames=FALSE), file = "Outputs/Table_A6b.tex")


# Table A9

m5 <- felm(lead_center ~ losepower + ternary_class_factor  + voteshare + outofcoalition|
             party+elect|0|party, data = dat, weights = dat$weights1[dat$cs == 1], subset = cs == 1)
m6 <-  felm(lead_center ~ losepower + ternary_class_factor + voteshare + outofcoalition|
              party+elect|0|party, data = dat, subset = twoparty == 1  & cs == 1, 
            weights = dat$weights1[dat$twoparty == 1 & dat$cs == 1])
m7 <-  felm(lead_center ~ losepower + ternary_class_factor + voteshare + outofcoalition|
              party+elect|0|party, data = dat, subset = twoparty == 0 & cs == 1, 
            weights = dat$weights1[dat$twoparty == 0 & dat$cs == 1])
m8 <- felm(lead_center ~ losepower * twoparty + ternary_class_factor  + voteshare + outofcoalition|
             party+elect|0|party, data = dat, weights = dat$weights1[dat$cs == 1], subset = cs == 1)

# p-value for hypothesis test that two party = > two party
summary(m8)$coef["losepower:twoparty", 4]

stargazer(m5, m6, m7,
          keep = "losepower",
          dep.var.labels = "Center Platform$\\_{t+1}$", 
          covariate.labels = "Loss of Power$\\_t$", 
          dep.var.caption = "",
          add.lines = list(c("Sample", "All", "$\\approx$ 2 party", "> 2 party"),
                           c("Voteshare$\\_t$", "yes", "yes", "yes"),
                           c("Platform$\\_t$ FE", "yes", "yes", "yes"),
                           c("Party FE",  "yes", "yes", "yes"),
                           c("Election FE", "yes", "yes", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"), 
          float = F,
          out = "Outputs/Table_A9.tex")

# Table A10
n5 <- felm(to_extreme ~  losepower * to_extreme_t_1 + ternary_class_factor  + voteshare + outofcoalition|
             party+elect|0|party, data = dat, subset = cs == 1, weights = dat$weights4[dat$cs == 1])
n6 <- felm(to_extreme ~  losepower * to_extreme_t_1 + ternary_class_factor  + voteshare + outofcoalition|
             party+elect|0|party, data = dat, subset = cs == 1 & twoparty == 1,
           weights = dat$weights4[dat$twoparty == 1 & dat$cs == 1])
n7 <- felm(to_extreme ~  losepower * to_extreme_t_1 + ternary_class_factor + voteshare + outofcoalition|
             party+elect|0|party, data = dat, subset = cs == 1 & twoparty == 0,
           weights = dat$weights4[dat$twoparty == 0 & dat$cs == 1])

stargazer(n5, n6, n7,
          keep = c("losepower", "to_extreme_t_1", "losepower:to_extreme_t_1"),
          dep.var.labels = "Center Platform$\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", "To Extreme$\\_{t-1}$", "Loss of power$\\_t \\times$ To Extreme$\\_{t-1}$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes", "yes", "yes"),
                           #    c("$\\Delta$ Voteshare$\\_t$", "yes", "yes", "yes"),
                           c("Platform$_t$ FE", "yes", "yes", "yes"),
                           c("Party FE", "yes", "yes", "yes"),
                           c("Election FE", "yes", "yes", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A10.tex")


# Table A11

o5 <- felm(absolute_shift ~ losepower * ls + absolute_shift_t_1 + 
             voteshare + outofcoalition|party + elect|0|party, data = dat, weights = dat$weights5)

o6 <- felm(absolute_shift ~ losepower * ls + absolute_shift_t_1 + 
             voteshare + outofcoalition|party + elect|0|party, data = dat, 
           weights = dat$weights5[dat$twoparty == 1], subset = twoparty == 1)

o7 <- felm(absolute_shift ~ losepower * ls + absolute_shift_t_1 + 
             voteshare + outofcoalition|party + elect|0|party, data = dat, 
           weights = dat$weights5[dat$twoparty == 0], subset = twoparty == 0)

o8 <- felm(absolute_shift ~ losepower * ls * twoparty + absolute_shift_t_1 + 
             voteshare + outofcoalition|party + elect|0|party, data = dat)

# p-value for panel B: 
summary(o8)$coef["losepower:ls:twoparty", 4]

q5 <- felm(to_extreme  ~ losepower * ls + to_extreme_t_1 + 
             voteshare + outofcoalition|party + elect|0|party, data = dat, weights = dat$weights5)

q6 <- felm(to_extreme ~ losepower * ls + to_extreme_t_1 + 
             voteshare + outofcoalition|party + elect|0|party, data = dat, 
           weights = dat$weights5[dat$twoparty == 1], subset = twoparty == 1)

q7 <- felm(to_extreme ~ losepower * ls + to_extreme_t_1 + 
             voteshare + outofcoalition|party + elect|0|party, data = dat, 
           weights = dat$weights5[dat$twoparty == 0], subset = twoparty == 0)

q8 <- felm(to_extreme  ~ losepower * ls *twoparty + to_extreme_t_1 + 
             voteshare + outofcoalition|party + elect|0|party, data = dat, weights = dat$weights5)

# p-value for panel A: 
summary(q8)$coef["losepower:ls:twoparty", 4]


stargazer(o5, o6, o7, 
          keep = c("losepower", "ls", "losepower:ls"),
          dep.var.labels = "Shift Magnitude$\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", "Large Selectorate", "Loss of power$\\_t \\times$ Large Selectorate$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes",  "yes", "yes"),
                           c("Out of Coalition$\\_{t}$", "yes", "yes", "yes"),
                           c("S$_t$ FE", "yes", "yes", "yes"),
                           c("Party FE",  "yes", "yes", "yes"),
                           c("Election FE",  "yes", "yes", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A11b.tex")

stargazer(q5, q6, q7, 
          keep = c("losepower", "ls", "losepower:ls"),
          dep.var.labels = "Shift Magnitude$\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", "Large Selectorate", "Loss of power$\\_t \\times$ Large Selectorate$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes",  "yes", "yes"),
                           c("Out of Coalition$\\_{t}$", "yes", "yes", "yes"),
                           c("S$_t$ FE", "yes", "yes", "yes"),
                           c("Party FE",  "yes", "yes", "yes"),
                           c("Election FE",  "yes", "yes", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A11a.tex")



# Table A12 

# From table A4
m3 <-  felm(lead_govt_party2 ~ losepower * TE + voteshare + outofcoalition|
              elect + party|0|party, subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])
#Disaggregated
m7 <-  felm(lead_govt_party2 ~ losepower * TE  + voteshare + outofcoalition|
              elect + party|0|party, data = dat, subset = cs == 1 & twoparty == 1,
            weights = dat$weights6[dat$twoparty == 1 & dat$cs == 1])
m8 <-  felm(lead_govt_party2 ~ losepower * TE  + voteshare + outofcoalition|
              elect + party|0|party, data = dat, subset = cs == 1 & twoparty == 0,
            weights = dat$weights6[dat$twoparty == 0 & dat$cs == 1])
# From table A4
m6 <-  felm(lead_govt_party2 ~ losepower * AS + voteshare + outofcoalition|
              elect + party|0|party, subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])
# Disaggregated
m9 <-  felm(lead_govt_party2 ~ losepower * AS  + voteshare + outofcoalition|
              elect + party|0|party, data = dat, subset = cs == 1 & twoparty == 1,
            weights = dat$weights6[dat$twoparty == 1 & dat$cs == 1])
m10 <-  felm(lead_govt_party2 ~ losepower * AS  + voteshare + outofcoalition|
               elect + party|0|party, data = dat, subset = cs == 1 & twoparty == 0,
             weights = dat$weights6[dat$twoparty == 0 & dat$cs == 1])

stargazer( m3, m7, m8, m6, m9, m10,
           keep = c("losepower", "TE", "losepower:TE", "AS", "losepower:AS"),
           dep.var.labels = "Government Party$\\_{t+2}$",
           covariate.labels = c("Loss of Power$\\_t$", 
                                "To Extreme$\\_{t+1}$", 
                                "Absolute Shift$\\{t+1}$",
                                "Loss of power$\\_t \\times$ To Extreme$\\_{t+1}$",
                                "Loss of power$\\_t \\times$ Absolute Shift$\\_{t+1}$"), 
           dep.var.caption = "",
           add.lines = list(c("Sample", "All", "$\\approx$ 2 party", ">2 party","All", "$\\approx$ 2 party", ">2 party"),
                            c("Voteshare$\\_t$", "yes", "yes", "yes", "yes", "yes", "yes"),
                            c("Party FE", "yes", "yes", "yes", "yes", "yes", "yes"),
                            c("Election FE", "yes", "yes", "yes", "yes", "yes", "yes")),
           omit.stat = c("f", 
                         "ser",
                         "rsq",
                         "adj.rsq"),
           float = F,
           out = "Outputs/Table_A12.tex")

# Table A13

n1 <- felm(lead_center ~ losepower + c_losepower + ternary_class_factor + voteshare + outofcoalition|
             0|0|party, subset = cs == 1, data = dat,weights = dat$weights1[dat$cs == 1])
n2 <- felm(lead_center ~ losepower + c_losepower + ternary_class_factor + voteshare + outofcoalition|
             elect|0|party, subset = cs == 1, data = dat,weights = dat$weights1[dat$cs == 1])
n3 <- felm(lead_center ~ losepower + c_losepower + ternary_class_factor + voteshare + outofcoalition|
             party|0|party, subset = cs == 1, data = dat,weights = dat$weights1[dat$cs == 1])
n4 <- felm(lead_center ~ losepower + c_losepower + ternary_class_factor + voteshare + outofcoalition|
             decade + party|0|party, subset = cs == 1, data = dat,weights = dat$weights1[dat$cs == 1])
n5 <- felm(lead_center ~ losepower + + c_losepower +ternary_class_factor + voteshare + outofcoalition|
             party+elect|0|party, subset = cs == 1, data = dat,weights = dat$weights1[dat$cs == 1])

stargazer(n1, n2, n3, n4, n5, 
          keep = c("losepower", "c_losepower"),
          dep.var.labels = "Center Platform\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", "Coalition Loss of Power$\\_t$"),
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes", "yes", "yes", "yes", "yes"),
                           c("Platform$\\_t$ FE", "yes", "yes", "yes", "yes", "yes"),
                           c("Party FE", "", "", "yes", "yes", "yes"),
                           c("Decade FE", "", "", "", "yes", ""),
                           c("Election FE", "", "yes", "", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A13.tex")

# Table A14

o1 <-  felm(to_extreme ~ losepower + c_losepower + to_extreme_t_1   + voteshare + outofcoalition|
              0|0|party, data = dat,weights = dat$weights4)
o2 <- felm(to_extreme ~ losepower * to_extreme_t_1 + c_losepower * to_extreme_t_1  + voteshare + outofcoalition|
             0|0|party, data = dat,weights = dat$weights4)
o3 <- felm(to_extreme ~ losepower * to_extreme_t_1 + c_losepower * to_extreme_t_1 + ternary_class_factor  + voteshare + outofcoalition|
             elect|0|party, data = dat,weights = dat$weights4)
o4 <- felm(to_extreme ~  losepower * to_extreme_t_1 + c_losepower * to_extreme_t_1 + ternary_class_factor  + voteshare + outofcoalition|
             party|0|party, data = dat,weights = dat$weights4)
o5 <- felm(to_extreme ~  losepower * to_extreme_t_1 + c_losepower * to_extreme_t_1 + ternary_class_factor  + voteshare + outofcoalition|
             decade + party|0|party, data = dat,weights = dat$weights4)
o6 <- felm(to_extreme ~  losepower * to_extreme_t_1 + c_losepower * to_extreme_t_1 + ternary_class_factor  + voteshare + outofcoalition|
             party+elect|0|party, data = dat,weights = dat$weights4)


stargazer(o1, o2, o3, o4, o5, o6, 
          keep = c("losepower", "c_losepower", "to_extreme_t_1", "losepower:to_extreme_t_1", "c_losepower:to_extreme_t_1"),
          dep.var.labels = "To Extreme$\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", "Coalition Loss of Power$\\_t$, $To Extreme$\\_{t-1}$", 
                               "Loss of power$\\_t \\times$ To Extreme$\\_{t-1}$", "Coal loss of power$\\_t \\times$ To Extreme$\\_{t-1}$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes", "yes", "yes", "yes", "yes", "yes"),
                           c("Party FE", "", "", "", "yes", "yes", "yes"),
                           c("Decade FE", "", "", "", "", "yes", ""),
                           c("Election FE", "", "", "yes", "", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A14.tex")



# Table A15

o1 <- felm(absolute_shift ~ losepower * ls + c_losepower * ls + absolute_shift_t_1 + 
             voteshare + outofcoalition|0|0|party, data = dat, weights = dat$weights5)

o2 <- felm(absolute_shift ~ losepower * ls  + c_losepower * ls + absolute_shift_t_1 + 
             voteshare + outofcoalition|elect|0|party, data = dat, weights = dat$weights5)

o3 <- felm(absolute_shift ~ losepower * ls + c_losepower * ls  + absolute_shift_t_1 + 
             voteshare + outofcoalition|party|0|party, data = dat, weights = dat$weights5)

o4 <- felm(absolute_shift ~ losepower * ls + c_losepower * ls  + absolute_shift_t_1 + 
             voteshare + outofcoalition|party + decade|0|party, data = dat, weights = dat$weights5)

o5 <- felm(absolute_shift ~ losepower * ls + c_losepower * ls  + absolute_shift_t_1 + 
             voteshare + outofcoalition|party + elect|0|party, data = dat, weights = dat$weights5)


q1 <- felm(to_extreme ~ losepower * ls + c_losepower * ls  + to_extreme_t_1 + 
             voteshare + outofcoalition|0|0|party, data = dat, weights = dat$weights5)

q2 <- felm(to_extreme  ~ losepower * ls  + c_losepower * ls + to_extreme_t_1 + 
             voteshare + outofcoalition|elect|0|party, data = dat, weights = dat$weights5)

q3 <- felm(to_extreme  ~ losepower * ls  + c_losepower * ls + to_extreme_t_1 + 
             voteshare + outofcoalition|party|0|party, data = dat, weights = dat$weights5)

q4 <- felm(to_extreme  ~ losepower * ls  + c_losepower * ls + to_extreme_t_1 + 
             voteshare + outofcoalition|party + decade|0|party, data = dat, weights = dat$weights5)

q5 <- felm(to_extreme  ~ losepower * ls  + c_losepower * ls + to_extreme_t_1 + 
             voteshare + outofcoalition|party + elect|0|party, data = dat, weights = dat$weights5)


stargazer(o1, o2, o3, o4, o5, 
          keep = c("losepower", "ls", "c_losepower", "losepower:ls"),
          dep.var.labels = "Shift Magnitude$\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", "Large Selectorate", 
                               "Coal. loss of power$\\_t$", "Loss of power$\\_t \\times$ Large Selectorate$",
                               "Coal. loss of power$\\_t \\times$ Large Selectorate$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes",  "yes", "yes", "yes", "yes"),
                           c("Out of Coalition$\\_{t}$", "yes", "yes", "yes", "yes", "yes"),
                           c("S$_t$ FE", "yes", "yes", "yes", "yes", "yes"),
                           c("Party FE",  "", "", "yes", "yes", "yes"),
                           c("Decade FE",  "", "", "", "yes", ""),
                           c("Election FE",  "", "yes", "", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A15b.tex")


stargazer(q1, q2, q3, q4, q5, 
          keep = c("losepower", "ls", "c_losepower", "losepower:ls"),
          dep.var.labels = "To Extreme$\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", "Large Selectorate", 
                               "Coal. loss of power$\\_t$", "Loss of power$\\_t \\times$ Large Selectorate$",
                               "Coal. loss of power$\\_t \\times$ Large Selectorate$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes",  "yes", "yes", "yes", "yes"),
                           c("Out of Coalition$\\_{t}$", "yes", "yes", "yes", "yes", "yes"),
                           c("S$_t$ FE", "yes", "yes", "yes", "yes", "yes"),
                           c("Party FE",  "", "", "yes", "yes", "yes"),
                           c("Decade FE",  "", "", "", "yes", ""),
                           c("Election FE",  "", "yes", "", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A15a.tex")


# Table A16 -- does not use data

# Table A17

t1 <- felm(lead_center ~ losepower * delta_voteshare + ternary_class_factor  + voteshare + outofcoalition|
             0|0|party, data = dat, weights = dat$weights1)
t2 <- felm(lead_center ~ losepower * delta_voteshare + ternary_class_factor  + voteshare + outofcoalition|
             elect|0|party, data = dat, weights = dat$weights1)
t3 <- felm(lead_center ~ losepower* delta_voteshare  + ternary_class_factor  + voteshare + outofcoalition|
             party|0|party, data = dat, weights = dat$weights1)
t4 <- felm(lead_center ~ losepower * delta_voteshare + ternary_class_factor  + voteshare + outofcoalition|
             decade + party|0|party, data = dat, weights = dat$weights1)
t5 <- felm(lead_center ~ losepower* delta_voteshare  + ternary_class_factor  + voteshare + outofcoalition|
             party+elect|0|party, data = dat, weights = dat$weights1)

u1 <- felm(lead_center ~ losepower * I(delta_voteshare < 0) + ternary_class_factor  + voteshare + outofcoalition|
             0|0|party, data = dat, weights = dat$weights1)
u2 <- felm(lead_center ~ losepower * I(delta_voteshare < 0) + ternary_class_factor  + voteshare + outofcoalition|
             elect|0|party, data = dat, weights = dat$weights1)
u3 <- felm(lead_center ~ losepower* I(delta_voteshare < 0)  + ternary_class_factor  + voteshare + outofcoalition|
             party|0|party, data = dat, weights = dat$weights1)
u4 <- felm(lead_center ~ losepower * I(delta_voteshare < 0) + ternary_class_factor  + voteshare + outofcoalition|
             decade + party|0|party, data = dat, weights = dat$weights1)
u5 <- felm(lead_center ~ losepower* I(delta_voteshare < 0)  + ternary_class_factor  + voteshare + outofcoalition|
             party+elect|0|party, data = dat, weights = dat$weights1)

# Regression table
stargazer(t1, t2, t3, t4, t5,
          keep = c("losepower", "delta_voteshare"),
          dep.var.labels = "Center Platform$\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", "$\\Delta$ Voteshare$\\_t$", "Loss of Power$\\_t \\times \\Delta$ Voteshare$\\_t"),
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes", "yes", "yes", "yes", "yes"),
                           c("Platform$\\_t$ FE", "yes", "yes", "yes", "yes", "yes"),
                           c("Party FE", "", "", "yes", "yes", "yes"),
                           c("Decade FE", "", "", "", "yes", ""),
                           c("Election FE", "", "yes", "", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A17a.tex")


stargazer(u1, u2, u3, u4, u5,
          keep = c("losepower", "delta_voteshare"),
          dep.var.labels = "Center Platform$\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", "$Loss of Voteshare$\\_t$", "Loss of Power$\\_t \\times$ Loss of Voteshare$\\_t"),
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes", "yes", "yes", "yes", "yes"),
                           c("Platform$\\_t$ FE", "yes", "yes", "yes", "yes", "yes"),
                           c("Party FE", "", "", "yes", "yes", "yes"),
                           c("Decade FE", "", "", "", "yes", ""),
                           c("Election FE", "", "yes", "", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A17b.tex")

# Table A18

t0 <-  felm(to_extreme ~ losepower * delta_voteshare + to_extreme_t_1  + voteshare + outofcoalition|
              0|0|party, data = dat,weights = dat$weights4)
t1 <- felm(to_extreme ~ losepower * to_extreme_t_1  * delta_voteshare + voteshare + outofcoalition|
             0|0|party, data = dat,weights = dat$weights4)
t2 <- felm(to_extreme ~ losepower * to_extreme_t_1  * delta_voteshare+ ternary_class_factor + voteshare + outofcoalition|
             elect|0|party, data = dat,weights = dat$weights4)
t3 <- felm(to_extreme ~  losepower * to_extreme_t_1 * delta_voteshare + ternary_class_factor  + voteshare + outofcoalition|
             party|0|party, data = dat,weights = dat$weights4)
t4 <- felm(to_extreme ~  losepower * to_extreme_t_1  * delta_voteshare+ ternary_class_factor  + voteshare + outofcoalition|
             decade + party|0|party, data = dat,weights = dat$weights4)
t5 <- felm(to_extreme ~  losepower * to_extreme_t_1  * delta_voteshare+ ternary_class_factor  + voteshare + outofcoalition|
             party+elect|0|party, data = dat,weights = dat$weights4)


u0 <-  felm(to_extreme ~ losepower * I(delta_voteshare > 0) + to_extreme_t_1  + voteshare + outofcoalition|
              0|0|party, data = dat,weights = dat$weights4)
u1 <- felm(to_extreme ~ losepower * to_extreme_t_1  *  I(delta_voteshare > 0) + voteshare + outofcoalition|
             0|0|party, data = dat,weights = dat$weights4)
u2 <- felm(to_extreme ~ losepower * to_extreme_t_1  *  I(delta_voteshare > 0) + ternary_class_factor + voteshare + outofcoalition|
             elect|0|party, data = dat,weights = dat$weights4)
u3 <- felm(to_extreme ~  losepower * to_extreme_t_1 *  I(delta_voteshare > 0) + ternary_class_factor  + voteshare + outofcoalition|
             party|0|party, data = dat,weights = dat$weights4)
u4 <- felm(to_extreme ~  losepower * to_extreme_t_1  *  I(delta_voteshare > 0) + ternary_class_factor  + voteshare + outofcoalition|
             decade + party|0|party, data = dat,weights = dat$weights4)
u5 <- felm(to_extreme ~  losepower * to_extreme_t_1  *  I(delta_voteshare > 0) + ternary_class_factor  + voteshare + outofcoalition|
             party+elect|0|party, data = dat,weights = dat$weights4)




stargazer(t0, t1, t2, t3, t4, t5, 
          keep = c("losepower", "to_extreme_t_1", "delta_voteshare"),
          dep.var.labels = "To Extreme$\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", 
                               "$\\Delta$ Voteshare$\\_t$",
                               "To Extreme$\\_{t-1}$", 
                               "Loss of power$\\_t \\times$ To Extreme$\\_{t-1}$",
                               "Loss of power$\\_t \\times \\Delta$ Voteshare$\\_t$",
                               "To Extreme$\\_{t-1}\\times \\Delta$ Voteshare$\\_t$",
                               "Loss of power$\\_t \\times$ To Extreme$\\_{t-1} \\times \\Delta$ Voteshare$\\_t$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes", "yes", "yes", "yes", "yes", "yes"),
                        
                           c("Platform$_t$ FE", "yes", "yes", "yes", "yes", "yes", "yes"),
                           c("Party FE", "", "", "", "yes", "yes", "yes"),
                           c("Decade FE", "", "", "", "", "yes", ""),
                           c("Election FE", "", "", "yes", "", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A18a.tex")


stargazer(u0, u1, u2, u3, u4, u5, 
          keep = c("losepower", "to_extreme_t_1", "delta_voteshare"),
          dep.var.labels = "To Extreme$\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", 
                               "Loss of Voteshare$\\_t$",
                               "To Extreme$\\_{t-1}$", 
                               "Loss of power$\\_t \\times$ To Extreme$\\_{t-1}$",
                               "Loss of power$\\_t \\times$ Loss of Voteshare$\\_t$",
                               "To Extreme$\\_{t-1}\\times$ Loss of Voteshare$\\_t$",
                               "Loss of power$\\_t \\times$ To Extreme$\\_{t-1} \\times$ Loss of Voteshare$\\_t$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes", "yes", "yes", "yes", "yes", "yes"),
                           
                           c("Platform$_t$ FE", "yes", "yes", "yes", "yes", "yes", "yes"),
                           c("Party FE", "", "", "", "yes", "yes", "yes"),
                           c("Decade FE", "", "", "", "", "yes", ""),
                           c("Election FE", "", "", "yes", "", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A18b.tex")


# Table A19



t1 <- felm(absolute_shift ~ losepower * ls * delta_voteshare + absolute_shift_t_1 + 
             voteshare + outofcoalition|0|0|party, data = dat, weights = dat$weights5)

t2 <- felm(absolute_shift ~ losepower * ls * delta_voteshare  + absolute_shift_t_1 + 
             voteshare + outofcoalition|elect|0|party, data = dat, weights = dat$weights5)

t3 <- felm(absolute_shift ~  losepower * ls * delta_voteshare  + absolute_shift_t_1 + 
             voteshare + outofcoalition|party|0|party, data = dat, weights = dat$weights5)

t4 <- felm(absolute_shift ~  losepower * ls * delta_voteshare  + absolute_shift_t_1 + 
             voteshare + outofcoalition|party + decade|0|party, data = dat, weights = dat$weights5)

t5 <- felm(absolute_shift ~  losepower * ls * delta_voteshare  + absolute_shift_t_1 + 
             voteshare + outofcoalition|party + elect|0|party, data = dat, weights = dat$weights5)


u1 <- felm(absolute_shift ~ losepower * ls * I(delta_voteshare >0) + absolute_shift_t_1 + 
             voteshare + outofcoalition|0|0|party, data = dat, weights = dat$weights5)

u2 <- felm(absolute_shift ~ losepower * ls * I(delta_voteshare >0) + absolute_shift_t_1 + 
             voteshare + outofcoalition|elect|0|party, data = dat, weights = dat$weights5)

u3 <- felm(absolute_shift ~  losepower * ls * I(delta_voteshare >0)  + absolute_shift_t_1 + 
             voteshare + outofcoalition|party|0|party, data = dat, weights = dat$weights5)

u4 <- felm(absolute_shift ~  losepower * ls * I(delta_voteshare >0) + absolute_shift_t_1 + 
             voteshare + outofcoalition|party + decade|0|party, data = dat, weights = dat$weights5)

u5 <- felm(absolute_shift ~  losepower * ls * I(delta_voteshare >0)  + absolute_shift_t_1 + 
             voteshare + outofcoalition|party + elect|0|party, data = dat, weights = dat$weights5)

stargazer(t1, t2, t3, t4, t5, 
          keep = c("losepower", "ls", "delta"),
          dep.var.labels = "Shift Magnitude$\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", "Large Selectorate$\\_t$", "$\\Delta$ Voteshare$\\_t$",
                               "Loss of power$\\_t \\times$ Large Selectorate$\\_t$",
                               "Loss of power$\\_t \\times \\Delta$ Voteshare$\\_t$",
                               "Large selectorate$\\_t \\times \\Delta$ Voteshare$\\_t$",
                               "Loss of power$\\_t \\times $ Large Selectorate$\\_t \\times \\Delta$ Voteshare$\\_t$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes",  "yes", "yes", "yes", "yes"),
                           c("Out of Coalition$\\_{t}$", "yes", "yes", "yes", "yes", "yes"),
                           c("S$_t$ FE", "yes", "yes", "yes", "yes", "yes"),
                           c("Party FE",  "", "", "yes", "yes", "yes"),
                           c("Decade FE",  "", "", "", "yes", ""),
                           c("Election FE",  "", "yes", "", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A19a.tex")

stargazer(u1, u2, u3, u4, u5, 
          keep = c("losepower", "ls", "delta"),
          dep.var.labels = "Shift Magnitude$\\_{t+1}$", 
          covariate.labels = c("Loss of Power$\\_t$", "Large Selectorate$\\_t$", "Loss of Voteshare$\\_t$",
                               "Loss of power$\\_t \\times$ Large Selectorate$\\_t$",
                               "Loss of power$\\_t \\times $ Loss of Voteshare$\\_t$",
                               "Large selectorate$\\_t \\times$ Loss of Voteshare $\\_t$",
                               "Loss of power$\\_t \\times $ Large Selectorate$\\_t \\times$ Loss of Voteshare$\\_t$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes",  "yes", "yes", "yes", "yes"),
                           c("Out of Coalition$\\_{t}$", "yes", "yes", "yes", "yes", "yes"),
                           c("S$_t$ FE", "yes", "yes", "yes", "yes", "yes"),
                           c("Party FE",  "", "", "yes", "yes", "yes"),
                           c("Decade FE",  "", "", "", "yes", ""),
                           c("Election FE",  "", "yes", "", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A19b.tex")


# Table A20

t1 <-  felm(lead_govt_party2 ~ losepower * TE * delta_voteshare + voteshare + outofcoalition|
              elect|0|party, subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])
t2 <-  felm(lead_govt_party2 ~ losepower * TE* delta_voteshare +voteshare + outofcoalition|
              party|0|party, subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])
t3 <-  felm(lead_govt_party2 ~ losepower * TE* delta_voteshare  + voteshare+ outofcoalition|
              elect + party|0|party, subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])
t4 <-  felm(lead_govt_party2 ~ losepower * AS * delta_voteshare+ voteshare + outofcoalition|
              elect|0|party,  subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])
t5 <-  felm(lead_govt_party2 ~ losepower * AS* delta_voteshare + voteshare + outofcoalition|
              party|0|party, subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])
t6 <-  felm(lead_govt_party2 ~ losepower * AS* delta_voteshare + voteshare + outofcoalition|
              elect + party|0|party, subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])

u1 <-  felm(lead_govt_party2 ~ losepower * TE * I(delta_voteshare > 0) + voteshare + outofcoalition|
              elect|0|party, subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])
u2 <-  felm(lead_govt_party2 ~ losepower * TE *  I(delta_voteshare > 0)  +voteshare+ outofcoalition|
              party|0|party, subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])
u3 <-  felm(lead_govt_party2 ~ losepower * TE * I(delta_voteshare > 0) + voteshare + outofcoalition|
              elect + party|0|party, subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])
u4 <-  felm(lead_govt_party2 ~ losepower * AS * I(delta_voteshare > 0)+ voteshare + outofcoalition|
              elect|0|party,  subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])
u5 <-  felm(lead_govt_party2 ~ losepower * AS* I(delta_voteshare > 0) + voteshare + outofcoalition|
              party|0|party, subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])
u6 <-  felm(lead_govt_party2 ~ losepower * AS* I(delta_voteshare > 0) + voteshare + outofcoalition|
              elect + party|0|party, subset = cs == 1, data = dat, 
            weights = dat$weights6[dat$cs == 1])


stargazer(t1, t2, t3, t4, t5, t6,
          keep = c("losepower", "TE", "AS", "delta"),
          dep.var.labels = "Government Party$\\_{t+2}$",
          covariate.labels = c("Loss of Power$\\_t$", 
                               "To Extreme$\\_{t+1}$", 
                               "Absolute Shift$\\{t+1}$",
                               "$\\Delta$ Voteshare$\\_t}$",
                               "Loss of power$\\_t \\times$ To Extreme$\\_{t+1}$",
                               "Loss of power$\\_t \\times$ Absolute Shift$\\_{t+1}$",
                               "Loss of power$\\_t \\times \\Delta$ Voteshare$\\_t$",
                               "To Extreme$\\_{t+1}\\times \\Delta$ Voteshare$\\_t$",
                               "Loss of power$\\_t \\times$ To Extreme$\\_{t+1}\\times \\Delta$ Voteshare$\\_t$",
                               "Absolute Shift\\_{t+1}\\times \\Delta$ Voteshare$\\_t$",
                               "Loss of power$\\_t \\times$ Absolute Shift$\\_{t+1}\\times \\Delta$ Voteshare$\\_t$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes", "yes", "yes", "yes", "yes", "yes"),
                           c("Platform$_t$ FE", "yes", "yes", "yes", "yes", "yes", "yes"),
                           c("Party FE", "", "yes", "yes", "", "yes", "yes"),
                           c("Election FE", "yes", "", "yes", "yes", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A20a.tex")

stargazer(u1, u2, u3, u4, u5, u6,
          keep = c("losepower", "TE", "AS", "delta"),
          dep.var.labels = "Government Party$\\_{t+2}$",
          covariate.labels = c("Loss of Power$\\_t$", 
                               "To Extreme$\\_{t+1}$", 
                               "Absolute Shift$\\{t+1}$",
                               "Loss of Voteshare$\\_t}$",
                               "Loss of power$\\_t \\times$ To Extreme$\\_{t+1}$",
                               "Loss of power$\\_t \\times$ Absolute Shift$\\_{t+1}$",
                               "Loss of power$\\_t \\times$ Loss of Voteshare$\\_t$",
                               "To Extreme$\\_{t+1}\\times$ Loss of Voteshare$\\_t$",
                               "Loss of power$\\_t \\times$ To Extreme$\\_{t+1}\\times$ Loss of Voteshare$\\_t$",
                               "Absolute Shift\\_{t+1}\\times \\Delta$ Voteshare$\\_t$",
                               "Loss of power$\\_t \\times$ Absolute Shift$\\_{t+1}\\times$ Loss of Voteshare$\\_t$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "yes", "yes", "yes", "yes", "yes", "yes"),
                           c("Platform$_t$ FE", "yes", "yes", "yes", "yes", "yes", "yes"),
                           c("Party FE", "", "yes", "yes", "", "yes", "yes"),
                           c("Election FE", "yes", "", "yes", "yes", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A20b.tex")


# Table A21
ingovt <- filter(dat, main_sample == 1)
r1 <-  felm(lead_center ~ losepower |
              0|0|party, data = ingovt)
r2 <-  felm(lead_center ~ losepower  + voteshare + outofcoalition +ternary_class_factor|
              0|0|party, data = ingovt)
r3 <-  felm(lead_center ~ losepower   + voteshare + outofcoalition + ternary_class_factor|
              country|0|party, data = ingovt)
r4 <-  felm(lead_center ~ losepower + voteshare + outofcoalition + ternary_class_factor|
              party|0|party, data = ingovt)

stargazer(r1, r2, r3, r4,
          keep = c("losepower"),
          dep.var.labels = "Center Platform$\\_{t+1}$",
          covariate.labels = c("Loss of Power$\\_t$"),
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "", "yes", "yes", "yes"),
                           c("Out of Coalition$\\_t$", "yes", "yes", "yes", "yes"),
                           c("Platform$_t$ FE", "yes", "yes", "yes", "yes"),
                           c("Country FE", "", "", "yes", ""),
                           c("Party FE", "", "", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A21.tex")

# Table A22

r1 <-  felm(lead_govt_party2 ~ losepower * TE |
              0|0|party, data = ingovt)
r2 <-  felm(lead_govt_party2 ~ losepower * TE  + voteshare + outofcoalition|
              0|0|party, data = ingovt)
r3 <-  felm(lead_govt_party2 ~ losepower * TE  + voteshare + outofcoalition|
              country|0|party, data = ingovt)
r4 <-  felm(lead_govt_party2 ~ losepower * TE  + voteshare + outofcoalition|
              party|0|party, data = ingovt)

stargazer(r1, r2, r3, r4,
          keep = c("losepower", "TE", "losepower:TE"),
          dep.var.labels = "Government Party after election$\\_{t+1}$",
          covariate.labels = c("Loss of Power$\\_t$", 
                               "To Extreme$\\_{t+1}$", 
                               "Loss of power$\\_t \\times$ To Extreme$\\_{t+1}$"), 
          dep.var.caption = "",
          add.lines = list(c("Voteshare$\\_t$", "", "yes", "yes", "yes"),
                           c("Country FE", "", "", "yes", ""),
                           c("Party FE", "", "", "", "yes")),
          omit.stat = c("f", 
                        "ser",
                        "rsq",
                        "adj.rsq"),
          float = F,
          out = "Outputs/Table_A22.tex")



# Appendix figures --------------------------------------------------------

# Figure A1: 

lh_dat <- left_join(dat, 
                    dplyr::group_by(lh, party) %>%
                      summarize(lh = mean(partyorgalt)) %>%
                      filter(!is.na(lh))) %>%
  ungroup() %>%
  filter(decade > 1950) %>%
  group_by(decade) %>%
  summarize(cor = cor(lh, ls, use = "complete.obs"))

figA1 <-  lh_dat %>%
  ggplot(aes(x = decade, y = cor)) +
  geom_point(size = 2) +
  geom_line(lty = 2) + 
  geom_vline(xintercept = 1992, col = "red", lty = 3) +
  scale_y_continuous("Linear correlation between Schumacher et al. (2013)\nParty organization and Large Selectorate", limits = c(-.15, 0)) +
  scale_x_continuous("Decade") +
  theme_minimal() 
  
ggsave(figA1, file = "Outputs/Figure_A1.pdf", width = 7, height = 4)

# Figure A2:
bw_robustness <- function(bw){
  df <- filter(dat, cs == 1) %>%
    arrange(party, edate) %>%
    mutate(tcf = ifelse(rile > ctry_mean + bw * ctry_sd, 3, 
                        ifelse(rile > ctry_mean - bw * ctry_sd, 2,
                               ifelse(rile < ctry_mean - bw * ctry_sd, 1, NA))),
           tcfb = ifelse(rile > ctry_rile_ma + bw * ctry_rile_sd2, 3, 
                         ifelse(rile > ctry_rile_ma - bw * ctry_rile_sd2, 2,
                                ifelse(rile < ctry_rile_ma - bw * ctry_rile_sd2, 1, NA))),
           tcfa = ifelse(tcf == 3 & left == 1 | tcf==1 & left == 0, 2, tcf)) %>%
    group_by(party) %>% 
    mutate(lc = 1 * (lead(tcf, 1) == 2),
           lcb = 1 * (lead(tcfb, 1)==2),
           lca = 1 * (lead(tcfa, 1)==2))
  
  m1 <- felm(lc ~ losepower + ternary_class_factor  + voteshare + outofcoalition|
               party+elect|0|party, data = df, weights = df$weights1)
  
  m2 <- felm(lca ~ losepower + ternary_class_factor  + voteshare + outofcoalition|
               party+elect|0|party, data = df, weights = df$weights1)
  
  m3 <- felm(lcb ~ losepower + ternary_class_factor  + voteshare + outofcoalition|
               party+elect|0|party, data = df, weights = df$weights1)
  
  ests <- c(summary(m1)$coef[1, 1:2], mean(df$lc, na.rm = T), 
            summary(m2)$coef[1,1:2], mean(df$lca, na.rm = T), 
            summary(m3)$coef[1, 1:2], mean(df$lcb, na.rm = T))
  return(ests)
}


out <- lapply(X = seq(from = 0.05, to =1, by = .05), FUN = bw_robustness)


figA2 <- data.frame(matrix(unlist(out),ncol = 3, byrow = T)) %>%
  mutate(dv = rep(c("Main Operationalization", "Restricted Main","Moving Average"), 20), 
         bw = rep(seq(from = 0.05, to = 1, by = .05), each = 3)) %>%
  ggplot(aes(x = bw,  y = X1, col = X3)) + geom_point() + facet_grid(dv ~.) +
  geom_errorbar(aes(ymin = X1 - 1.96 * X2, ymax = X1 + 1.96 * X2), width = 0) +
  geom_errorbar(aes(ymin = X1 - 1.64 * X2, ymax = X1 + 1.64 * X2), width = 0, lwd = 1) +
  geom_hline(yintercept = 0, col = "red",lty = 3) + theme_minimal() + 
  scale_colour_gradient("Proportion of \nCenter Platforms") + xlab("Bandwidth") + ylab("Estimate")

ggsave(figA2, file = "Outputs/Figure_A2.pdf", width = 7, height = 7)


# Figures A3-A6:

perm_controls = c(
  'Last platform' = 'ternary_class_factor',
  'Change in voteshare' = 'delta_voteshare',
  'Vote share' = 'voteshare',
  'Loss of coalition' = 'outofcoalition',
  'Mean voter ideology'= 'p_mean_wvs_recentm + wvs_missing + p_mean_eb_recentm +
                          eb_missing + p_mean_ess_recentm + ess_missing'
)


perm_fe_controls <- c(
  'Election FE' = 'elect',
  'Country FE' = 'country',
  'Party FE' = 'party'
)


figA3 <- stability_plot(data = dat, 
                     lhs = 'lead_center', 
                     rhs = 'losepower', 
                     perm = perm_controls,
                     weights = "weights1",
                     perm_fe = perm_fe_controls,
                     cluster = 'party')

figA4 <- stability_plot(data = dat, 
                            lhs = 'lead_center', 
                            rhs = 'losepower', 
                            perm = perm_controls,
                            perm_fe = perm_fe_controls,
                            cluster = 'party')

figA5 <- stability_plot(data = dat, 
                     lhs = 'lead_alt_center', 
                     rhs = 'losepower', 
                     perm = perm_controls,
                     weights = "weights3",
                     perm_fe = perm_fe_controls,
                     cluster = 'party')

figA6 <- stability_plot(data = dat, 
                     lhs = 'lead_centerb', 
                     rhs = 'losepower', 
                     perm = perm_controls,
                     weights = "weights2",
                     perm_fe = perm_fe_controls,
                     cluster = 'party')

ggsave(figA3, filename = "Outputs/Figure_A3.pdf", width = 9, height =5)
ggsave(figA4, filename = "Outputs/Figure_A4.pdf", width = 9, height =5)
ggsave(figA5, filename = "Outputs/Figure_A5.pdf", width = 9, height =5)
ggsave(figA6, filename = "Outputs/Figure_A6.pdf", width = 9, height =5)


# Figures A7-A10
base_controls <- c(
  'Lose Power' = 'losepower',
  'To Extreme (lagged)' = 'to_extreme_t_1'
)

perm_fe_controls <- c(
  'Election FE' = 'elect',
  'Country FE' = 'country',
  'Party FE' = 'party'
)



dat %<>%
  mutate(lp_te = losepower * to_extreme_t_1,
         lp_te_lt = losepower* to_extreme_lt_t_1,
         mn_to_extreme = to_extreme - med_to_extreme)

perm_controls = c(
  'Last platform' = 'ternary_class_factor',
  'Change in voteshare' = 'delta_voteshare',
  'Vote share' = 'voteshare',
  'Loss of coalition' = 'outofcoalition',
  'Mean voter ideology'= 'p_mean_wvs_recentm + wvs_missing + p_mean_eb_recentm +eb_missing + p_mean_ess_recentm + ess_missing'
)


figA7 <- stability_plot(data = dat, 
                     lhs = 'to_extreme', 
                     rhs = 'lp_te', 
                     base = base_controls,
                     perm = perm_controls,
                     weights = "weights4",
                     perm_fe = perm_fe_controls,
                     cluster = 'party')

figA8 <- stability_plot(data = dat, 
                     lhs = 'to_extreme', 
                     rhs = 'lp_te', 
                     base = base_controls,
                     perm = perm_controls,
                     perm_fe = perm_fe_controls,
                     cluster = 'party')


base_controls_lt <- c(
  'Lose Power' = 'losepower',
  'To Extreme (lagged)' = 'to_extreme_lt_t_1'
)


figA9 <- stability_plot(data = dat, 
                     lhs = 'to_extreme_lt', 
                     rhs = 'lp_te_lt', 
                     base = base_controls_lt,
                     perm = perm_controls,
                     weights = "weights4",
                     perm_fe = perm_fe_controls,
                     cluster = 'party')

figA10 <- stability_plot(data = dat, 
                         lhs = 'mn_to_extreme', 
                         rhs = 'lp_te', 
                         base = base_controls_lt,
                         perm = perm_controls,
                         weights = "weights4",
                         perm_fe = perm_fe_controls,
                         cluster = 'party')
ggsave(figA7, file = "Outputs/Figure_A7.pdf", width = 10, height = 5)
ggsave(figA8, file = "Outputs/Figure_A8.pdf", width = 10, height = 5)
ggsave(figA9, file = "Outputs/Figure_A9.pdf", width = 10, height = 5)
ggsave(figA10, file = "Outputs/Figure_A10.pdf", width = 10, height = 5)


# Figures A11-A14


base_controls_select <- c(
  'Lose Power' = 'losepower',
  'Selectorate' = 'ls',
  'Absolute shift (lagged)' = 'absolute_shift_t_1'
)

base_controls_alt <- c(
  'Lose Power' = 'ls_lp',
  'Selectorate' = 'ls',
  'Absolute shift (lagged)' = 'absolute_shift_t_1'
)

dat %<>%
  mutate(ls_lp = ls * losepower,
         as_dif_med = absolute_shift - med_absolute_shift)


figA11a <- stability_plot(data = dat, 
                      lhs = 'absolute_shift', 
                      rhs = 'losepower', 
                      base = base_controls_alt,
                      perm = perm_controls,
                      weights = "weights5",
                      perm_fe = perm_fe_controls,
                      cluster = 'party')

figA11b <- stability_plot(data = dat, 
                      lhs = 'absolute_shift', 
                      rhs = 'ls_lp', 
                      base = base_controls_select,
                      perm = perm_controls,
                      weights = "weights5",
                      perm_fe = perm_fe_controls,
                      cluster = 'party')

ggsave(grid.arrange(figA11a, figA11b), file = "Outputs/Figure_A11.pdf", width = 10, height = 11)


figA12a <- stability_plot(data = dat, 
                      lhs = 'absolute_shift', 
                      rhs = 'losepower', 
                      base = base_controls_alt,
                      perm = perm_controls,
                      perm_fe = perm_fe_controls,
                      cluster = 'party')

figA12b <- stability_plot(data = dat, 
                      lhs = 'absolute_shift', 
                      rhs = 'ls_lp', 
                      base = base_controls_select,
                      perm = perm_controls,
                      perm_fe = perm_fe_controls,
                      cluster = 'party')

ggsave(grid.arrange(figA12a, figA12b), file = "Outputs/Figure_A12.pdf", width = 10, height = 11)



figA13a <- stability_plot(data = dat, 
                      lhs = 'absolute_shift_lt', 
                      rhs = 'losepower', 
                      base = base_controls_alt,
                      perm = perm_controls,
                      weights = "weights5",
                      perm_fe = perm_fe_controls,
                      cluster = 'party')

figA13b <- stability_plot(data = dat, 
                      lhs = 'absolute_shift_lt', 
                      rhs = 'ls_lp', 
                      base = base_controls_select,
                      perm = perm_controls,
                      weights = "weights5",
                      perm_fe = perm_fe_controls,
                      cluster = 'party')

ggsave(grid.arrange(figA13a, figA13b), file = "Outputs/Figure_A13.pdf", width = 10, height = 11)


figA14a <- stability_plot(data = dat, 
                      lhs = 'as_dif_med', 
                      rhs = 'losepower', 
                      base = base_controls_alt,
                      perm = perm_controls,
                      weights = "weights5",
                      perm_fe = perm_fe_controls,
                      cluster = 'party')

figA14b <- stability_plot(data = dat, 
                      lhs = 'as_dif_med', 
                      rhs = 'ls_lp', 
                      base = base_controls_select,
                      perm = perm_controls,
                      weights = "weights5",
                      perm_fe = perm_fe_controls,
                      cluster = 'party')

ggsave(grid.arrange(figA14a, figA14b), file = "Outputs/Figure_A14.pdf", width = 10, height = 11)



# Figures A15-A17

base_controls <- c(
  'Lose Power' = 'losepower',
  'To Extreme(/100)' = 'TE'
)

base_controls_lt <- c(
  'Lose Power' = 'losepower',
  'To Extreme (Logit transformed)' = 'to_extreme_lt'
)

perm_fe_controls <- c(
  'Election FE' = 'elect',
  'Country FE' = 'country',
  'Party FE' = 'party'
)

dat %<>%
  mutate(lp_TE = losepower * TE,
         lp_TE_lt = losepower* to_extreme_lt)

perm_controls = c(
  'Last platform' = 'ternary_class_factor',
  'Change in voteshare' = 'delta_voteshare',
  'Vote share' = 'voteshare',
  'Loss of coalition' = 'outofcoalition',
  'Mean voter ideology'= 'p_mean_wvs_recentm + wvs_missing + p_mean_eb_recentm +eb_missing + p_mean_ess_recentm + ess_missing'
)

figA15 <- stability_plot(data = dat, 
                   lhs = 'lead_govt_party2', 
                   rhs = 'lp_TE', 
                   base = base_controls,
                   perm = perm_controls,
                   weights = "weights6",
                   perm_fe = perm_fe_controls,
                   cluster = 'party')

figA16 <- stability_plot(data = dat, 
                    lhs = 'lead_govt_party2', 
                    rhs = 'lp_TE', 
                    base = base_controls,
                    perm = perm_controls,
                    perm_fe = perm_fe_controls,
                    cluster = 'party')


figA17 <- stability_plot(data = dat, 
                    lhs = 'lead_govt_party2', 
                    rhs = 'lp_TE_lt', 
                    base = base_controls_lt,
                    perm = perm_controls,
                    weights = "weights6",
                    perm_fe = perm_fe_controls,
                    cluster = 'party')

ggsave(figA15, file = "Outputs/Figure_A15.pdf", width = 10, height =5)
ggsave(figA16, file = "Outputs/Figure_A16.pdf", width = 10, height =5)
ggsave(figA17, file = "Outputs/Figure_A17.pdf", width = 10, height =5)



